package netty.c3;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.internal.logging.Log4J2LoggerFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import static netty.bytebuf.TestByteBuf.log;

public class HelloWorldServer {
    static Logger logger = LogManager.getLogger("mylog");
    public static void main(String[] args) {
        start();

    }

    private static void start() {
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap()
                    .group(boss, worker)
                    .channel(NioServerSocketChannel.class)
                    .childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(16, 16,16))
                    .childHandler(
                            new ChannelInitializer<NioSocketChannel>() {

                                @Override
                                protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                                    nioSocketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024));
                                    //  nioSocketChannel.pipeline().addLast(new FixedLengthFrameDecoder(10));
                                    nioSocketChannel.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                                   // nioSocketChannel.pipeline().addLast(new StringDecoder());
                                    nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                                        @Override
                                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                            logger.debug("connected:{}", ctx.channel());
                                            super.channelActive(ctx);
                                        }

                                        @Override
                                        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                                            logger.debug("disconnected:{}", ctx.channel());
                                            super.channelInactive(ctx);
                                        }

                                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                            logger.debug("read...{}", ctx.channel());
                                            ByteBuf buf = (ByteBuf) msg;
                                            log(buf);
                                        }
                                    });
                                }
                            }
                    );
            ChannelFuture channelFuture = serverBootstrap.bind(8080);
            logger.debug("{} binding...", channelFuture.channel());
            channelFuture.sync();
            logger.debug("{} bound...", channelFuture.channel());
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }
}
